package com.wang.auth.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wang.auth.mapper.SysUserMapper;
import com.wang.auth.service.SysRoleService;
import com.wang.auth.service.SysUserRoleService;
import com.wang.auth.service.SysUserService;
import com.wang.common.result.Result;
import com.wang.model.system.SysRole;
import com.wang.model.system.SysUser;
import com.wang.model.system.SysUserRole;
import com.wang.model.system.vo.SysUserQueryVo;
import com.wang.model.system.dto.SysUserDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author wang
 * @since 2023-04-17
 */
@Service
@Slf4j
public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> implements SysUserService {

    @Resource
    private SysUserMapper sysUserMapper;

    @Resource
    private SysUserRoleService sysUserRoleService;

    @Resource
    private SysRoleService roleService;

    @Override
    public void updateStatus(Long id, Integer status) {
        if (status == 0 || status == 1){
            SysUser sysUser = new SysUser();
            sysUser.setId(id);
            sysUser.setStatus(status);
            this.updateById(sysUser);
        }
    }

//    @Override
//    public Result index(Long page, Long limit, SysUserQueryVo sysUserQueryVo) {
//        Page<SysUser> sysUserPage = new Page<>(page,limit);
//        LambdaQueryWrapper<SysUser> sysUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
//        String userName = sysUserQueryVo.getKeyword();
//        String createTimeBegin = sysUserQueryVo.getCreateTimeBegin();
//        String createTimeEnd = sysUserQueryVo.getCreateTimeEnd();
//        log.info("用户名：{}",userName);
//        log.info("开始时间：{}",createTimeBegin);
//        log.info("结束时间：{}",createTimeEnd);
//        sysUserLambdaQueryWrapper.like(StringUtils.isNotBlank(userName),SysUser::getUsername,userName);
//        //ge代表>=  le代表<=
//        sysUserLambdaQueryWrapper.ge(StringUtils.isNotBlank(createTimeBegin),SysUser::getCreateTime,createTimeBegin);
//        sysUserLambdaQueryWrapper.le(StringUtils.isNotBlank(createTimeEnd),SysUser::getCreateTime,createTimeEnd);
//        return Result.success(this.page(sysUserPage,sysUserLambdaQueryWrapper));
//    }

    @Override
    public Result index(Page<SysUserDto> userDtoPage, SysUserQueryVo sysUserQueryVo) {
        Page<SysUserDto> sysUserDtoPage = sysUserMapper.selectPageAll(userDtoPage,sysUserQueryVo);
        List<SysUserDto> userDtoList = sysUserDtoPage.getRecords();
        userDtoList.stream().map(userDto -> {
            LambdaQueryWrapper<SysUserRole> sysUserRoleLambdaQueryWrapper = new LambdaQueryWrapper<>();
            sysUserRoleLambdaQueryWrapper.eq(SysUserRole::getUserId, userDto.getId());
            List<SysUserRole> sysUserRoleList = sysUserRoleService.list(sysUserRoleLambdaQueryWrapper);
            List<Long> roleIdList = new ArrayList<>();
            for (SysUserRole sysUserRole : sysUserRoleList) {
                roleIdList.add(sysUserRole.getRoleId());
            }
            //这里一定要判空，如果roleIdList为空，则使用listByIds会报错
            if (!roleIdList.isEmpty()) {
                List<SysRole> sysRoleList = roleService.listByIds(roleIdList);
                List<String> roleNameList = sysRoleList.stream().map(SysRole::getRoleName).collect(Collectors.toList());
                userDto.setRoleNameList(roleNameList);

            }
            return userDto;
        }).collect(Collectors.toList());
        sysUserDtoPage.setRecords(userDtoList);
        return Result.success(sysUserDtoPage);
    }

    @Override
    public SysUser getUserByUserName(String username) {
        LambdaQueryWrapper<SysUser> sysUserLambdaQueryWrapper = new LambdaQueryWrapper<>();
        sysUserLambdaQueryWrapper.eq(SysUser::getUsername,username);
        return this.getOne(sysUserLambdaQueryWrapper);
    }
}
